package com.customization.unionpay.market.workflowaction;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.maketEnum.InstructionEnum;
import com.customization.unionpay.market.util.ClearFundButtonRelated;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.PropUtils;
import weaver.generalledger.action.ZzgdExecuteInstrucitonAction;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.Map;

/**
 * @author sjm
 * @description 营销个税计提单预算校验相关
 */
public class MarketingPersonalTaxLadingVerifyAction implements Action {

    private static final Logger log = LoggerFactory.getLogger(MarketingPersonalTaxLadingVerifyAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {
        RequestManager rm = requestInfo.getRequestManager();
        String requestid = String.valueOf(rm.getRequestid());
        try {

            String tableName = rm.getBillTableName();          // 表单名称
            log.info("tableName:" + tableName);

            com.customization.unionpay.market.workflowaction.MarketingPersonalTaxLadingAction marketingPersonalTaxLadingAction = new MarketingPersonalTaxLadingAction();
            // 流程标题
            String requestName = requestInfo.getDescription();

            Map<String, Object> allFormData = com.customization.unionpay.market.util.FormDataUtils.getAllFormData(requestInfo);

            Map<String, String> mainMap = (Map<String, String>) allFormData.get("mainMap");
            // 所有明细表的值
            Map<String, Map<String, Map<String, String>>> detailMap = (Map<String, Map<String, Map<String, String>>>) allFormData.get("detailMap");

            // 主 业务日期        ywrq
            String ywrq = mainMap.get("ywrq");
            log.info("ywrq："+ywrq);
            // 主 预算控制期      yskzq
            String yskzq = mainMap.get("yskzq");
            log.info("yskzq："+yskzq);

            String ywrqStr = com.customization.unionpay.market.util.FormDataUtils.changeDateString("yyyy-MM-dd", ywrq, "yyyy");
            String yskzqStr = com.customization.unionpay.market.util.FormDataUtils.changeDateString("yyyy-MM-dd", yskzq, "yyyy");

            String bdh = mainMap.get("bdh");
            log.info("bdh："+bdh);
            if(bdh == null || "".equals(bdh)){
                bdh = "bdh";
            }

            JSONObject params = new JSONObject();
            String instruction = "";
            if(ywrqStr.equals(yskzqStr)){
                params = marketingPersonalTaxLadingAction.getParamsAddMiddle(requestName, yskzq, yskzqStr, mainMap, detailMap,bdh,bdh,tableName,requestid);
                instruction = com.customization.unionpay.market.maketEnum.InstructionEnum.ZY.toString();
            }else {
                params = marketingPersonalTaxLadingAction.getParamsAddMiddle(requestName, yskzq, yskzqStr, mainMap, detailMap,bdh,bdh,tableName,requestid);
                instruction = InstructionEnum.XMZY.toString();
            }

            log.info("预算校验拼接参数："+params);
            int yszjbFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "yszjb_form_id"));
            int yszjbFormmodeId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "yszjb_formmode_id"));
            String yszjbTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(yszjbFormId);
            log.info("yszjbTableName：" + yszjbTableName);
            String bId = FormDataUtils.addYuMiddleForm(yszjbFormId, yszjbFormmodeId, yszjbTableName, rm.getUser(), params);

            log.info("instruction："+instruction);
            ZzgdExecuteInstrucitonAction zzgdExecuteInstrucitonAction = new ZzgdExecuteInstrucitonAction();
            zzgdExecuteInstrucitonAction.setZl(instruction);
            String execute = zzgdExecuteInstrucitonAction.execute(requestInfo);
            if("0".equals(execute)){
                if(bId != null && !"".equals(bId)){
                    ClearFundButtonRelated.deleteByBillId(yszjbTableName, Integer.parseInt(bId));
                }
                String messageid = requestInfo.getRequestManager().getMessageid();
                log.info("messageid："+messageid);
                String messagecontent = requestInfo.getRequestManager().getMessagecontent();
                log.info("messagecontent："+messagecontent);
                return Action.FAILURE_AND_CONTINUE;
            }else {
                log.info("预算调用成功！");
            }
        } catch (Exception e) {
            // 异常报错是填写异常信息，阻止流程继续流转
            requestInfo.getRequestManager().setMessageid("10001");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
